/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Martin Mohr Pedersen
 */
public class Guest {
    
    private String name;
    private String cpr;
    private String gender;
    private Date birthdate;
    
    
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the cpr
     */
    public String getCpr() {
        return cpr;
    }

    /**
     * @param cpr the cpr to set
     */
    public void setCpr(String cpr) {
        this.cpr = cpr;
    }
    
    /**
     * Parses the given CPR string and sets the objects cpr, birthdate and gender based on this value.
     * @param cpr A valid CPR
     * @param extractValues 
     */
    public void parseCpr(String cpr) {
        
        setCpr(cpr);
        
        // find the gender
        int genderInt = Integer.parseInt(cpr.substring(9, 10));

        if(genderInt % 2 == 0) {
            // equal numbers are female
            setGender("F");
        } else {
            // odd numbers are male
            setGender("M");
        }

        // calculate the age
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy");

        String birthString = cpr.substring(0, 6);

        try {
            Date birthdate = sdf.parse(birthString);
            setBirthdate(birthdate);
        } catch (ParseException ex) {
            System.out.println("Could not parse birthdate for user.");
        }
        
    }

    /**
     * Returns this guests gender. M if Male or F if Female
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * M if Male or F if Female
     * @param gender
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return the birthdate
     */
    public Date getBirthdate() {
        return birthdate;
    }

    /**
     * @param birthdate the birthdate to set
     */
    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }
    
}
